<?php


namespace App\Service;

use App\Http\Requests\Request;
use Illuminate\Support\Facades\Storage;

/**
 * 图片上传方法
 * Class ImgUploadService
 * @package App\Service
 * return $img_path 图片路径
 */
class ImgUploadService
{
    public function img_upload(Request $request, $key = 'image', $is_qiniu = 0)
    {
        $imgs_path = [];
        foreach ($request->$key as $imgBase64) {
            if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $imgBase64, $res)) {
                //获取图片类型   
                $type = $res[2];

                //图片保存路径
                $new_file = "uploads/" . date('Ymd', time()) . '/';

                if (!file_exists($new_file)) {

                    mkdir($new_file, 0755, true);
                }
                //图片名字
                $new_file = $new_file . time() . '.' . $type;
                if (!file_put_contents($new_file, base64_decode(str_replace($res[1], '', $imgBase64)))) {
                    return false;
                }
                array_push($imgs_path,$new_file);
            }else{
                return false;
            }
        }
        return $new_file;
        
    }
}
